package org.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.reggie.common.BaseContext;
import org.reggie.common.R;
import org.reggie.entity.ShoppingCart;
import org.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        log.info("购物车数据:{}",shoppingCart);
        ShoppingCart cart = shoppingCartService.add(shoppingCart,session);
        return R.success(cart);
    }

    // 客户端的套餐或者是菜品数量减少设置


    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        log.info("购物车数据:{}",shoppingCart);
        ShoppingCart cart = shoppingCartService.sub(shoppingCart,session);
        return R.success(cart);
    }
// 查看购物车

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //这里就不要用BaseContext，具体为毛不太好说
        Long userId= (Long) session.getAttribute("user");
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    /*清空购物车*/
    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        log.info("清空购物车...");
        shoppingCartService.clean(session);
        return R.success("清空购物车成功");
    }

}
